#include <iostream>
#include <deque>
#include <cstring>
using namespace std;
int n,m;
const int N=510;
char p[N][N];
int x1,x2,y1,y2;
int dist[N][N];
typedef pair <int,int> PII;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
void bfs()
{
	deque<PII> q;
	q.push_front({x1,y1});
	memset(dist,-1,sizeof dist);
	dist[x1][y1]=0;
	while(q.size())
	{
		
		PII t=q.front();q.pop_front();
		int a=t.first;int b=t.second;
		if(a==x2&&b==y2)
		return;
		for(int k=0;k<4;k++)
		{
			int x=a+dx[k],y=b+dy[k];
			if(x<0||x>=n||y<0||y>=m)continue;
			int w=0;
			if(p[x][y]!=p[a][b])
			w=1;
			
			if(dist[x][y]==-1)
			{
			dist[x][y]=dist[a][b]+w;
		   if(w==0)q.push_front({x,y});
		   else q.push_back({x,y});	
	    	}
	    	
			else if(dist[a][b]+w<dist[x][y])
			dist[x][y]=dist[a][b]+w;
			
			
		}
	}
}
int main()
{
	while(cin>>n>>m,n&&m)
	{
	for(int i=0;i<n;i++)
	 {	
		for(int j=0;j<m;j++)
		{
			cin>>p[i][j];
		}
	 }
	 cin>>x1>>y1>>x2>>y2;
	 bfs();
	    cout<<dist[x2][y2]<<endl;
   }

	return 0;
}
